﻿using System.Web.Mvc;
using Sharp.Infrastructure;
using Sharp.Platform.Mvc.Models;
using System.Diagnostics;

namespace Sharp.Platform.Mvc.Filter
{
    /// <summary>
    /// 异常筛选器
    /// </summary>
    //[DebuggerStepThrough]
    public class ErrorExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext.Result is ViewResult || filterContext.Result is EmptyResult)
            {
                filterContext.Controller.ViewData.Model = new ExceptionViewModel(filterContext.Exception);
                filterContext.ExceptionHandled = true;

                filterContext.Result = new ViewResult()
                {
                    ViewName = "Error",
                    ViewData = filterContext.Controller.ViewData,
                };
            }
            else if (filterContext.Result is JsonResult)
            {
                filterContext.ExceptionHandled = true;
                filterContext.Result = new JsonResult()
                {
                    Data = new Result()
                    {
                        InnerException = filterContext.Exception
                    }
                };
            }
        }
    }
}
